package levelrewind.android.app.adapter;

import java.text.SimpleDateFormat;
import java.util.ArrayList;

import levelrewind.android.app.R;
import levelrewind.android.app.model.Util;
import levelrewind.android.app.vo.Battle;
import levelrewind.android.app.vo.BattlePerson;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

public class BattleHistoryListAdapter extends BaseAdapter {
	private ArrayList<Battle>  battles = new ArrayList<Battle>();
    private LayoutInflater mInflater;
    private Context c;
    
    public BattleHistoryListAdapter(Context context) {
        mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        c = context;
    }
    
    public void addItem(final Battle item) {
    	battles.add(item);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return battles.size();
    }

    @Override
    public Battle getItem(int position) {
        return battles.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        System.out.println("getView " + position + " " + convertView);
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.battle_history_layout, null);
            holder = new ViewHolder();
            holder.layout = (LinearLayout)convertView.findViewById(R.id.user_data);
            holder.txtName = (TextView)convertView.findViewById(R.id.battle_name);
            holder.txtPeriod = (TextView)convertView.findViewById(R.id.battle_period);
            holder.txtPerson1 = (TextView)convertView.findViewById(R.id.battle_person1);
            holder.txtPerson2 = (TextView)convertView.findViewById(R.id.battle_person2);
            
            holder.txtName.setTypeface(Util.customFont(c));
            holder.txtPeriod.setTypeface(Util.customFont(c));
            holder.txtPerson1.setTypeface(Util.customFont(c));
            holder.txtPerson2.setTypeface(Util.customFont(c));
            
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder)convertView.getTag();
        }        
        
        String DATE_FORMAT = "dd/MM/yyyy";       
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
        Battle b = battles.get(position);
        
        holder.txtName.setText(b.getName());
        
        String period = c.getString(R.string.battle_history_period, sdf.format(b.getStartDate()), sdf.format(b.getEndDate()));
        holder.txtPeriod.setText(period);
        
        BattlePerson p1 = b.getPersons()[0];
        String person1 = c.getString(R.string.battle_history_score, p1.getPerson().getName(), Util.formatNumber(p1.getScore()));
        holder.txtPerson1.setText(person1);
        
        BattlePerson p2 = b.getPersons()[1];
        String person2 = c.getString(R.string.battle_history_score, p2.getPerson().getName(), Util.formatNumber(p2.getScore()));
        holder.txtPerson2.setText(person2);

        return convertView;
    }

    public static class ViewHolder {
    	public LinearLayout layout;
        public TextView txtName;
        public TextView txtPeriod;
        public TextView txtPerson1;
        public TextView txtPerson2;
    }

}
